
## New Replication Miller - Data Prep


pacman::p_load(haven, tidyverse, texreg)

unique_id <- function(x, ...) {
  id_set <- x %>% select(...)
  id_set_dist <- id_set %>% distinct
  if (nrow(id_set) == nrow(id_set_dist)) {
    TRUE
  } else {
    non_unique_ids <- id_set %>% 
      filter(id_set %>% duplicated()) %>% 
      distinct()
    suppressMessages(
      inner_join(non_unique_ids, x) %>% arrange(...)
    )
  }
}
# load data


miller <- read_dta("Data//Replication/Miller/EAHumanDev_CPS_Replication.dta")

ead <-  readRDS("Data/Regimedata/Measures_merged.rds")


names(miller)



min(miller$year)


df <- left_join(miller, ead%>%rename(ccode = Final_Code), by = c("year", "ccode"))








# Prep Miller



##Create stock vars

df[,c("ea" , "dem",
      "RegType_lied_EA", "RegType_lied_DEM",
      "RegType_RoW_EA", "RegType_RoW_DEM",
      "Politytype_Anocracy", "Politytype_Democracy",
      "status_fh_PF", "status_fh_F",
      "AnckarRegtype_MP_Autocracy", "AnckarRegtype_Democracy",
      "HTW_RegType_MP_Autocracy", "HTW_RegType_Democracy",
      "RegType_magaloni_EA", "RegType_magaloni_DEM",
      "RegType_miller_EA", "RegType_miller_DEM")] <- lapply(df[,c("ea" , "dem",
                                                                            "RegType_lied_EA", "RegType_lied_DEM",
                                                                            "RegType_RoW_EA", "RegType_RoW_DEM",
                                                                            "Politytype_Anocracy", "Politytype_Democracy",
                                                                            "status_fh_PF", "status_fh_F",
                                                                            "AnckarRegtype_MP_Autocracy", "AnckarRegtype_Democracy",
                                                                            "HTW_RegType_MP_Autocracy", "HTW_RegType_Democracy",
                                                                            "RegType_magaloni_EA", "RegType_magaloni_DEM",
                                                                            "RegType_miller_EA", "RegType_miller_DEM")] , as.numeric)


df <- df %>%
  group_by(ccode) %>% arrange(year) %>%
  mutate(t = 2010-year)





df <- df %>%
  group_by(ccode) %>% arrange(year) %>% mutate(RegType_lied_EA_hist = cumsum(.99^t * RegType_lied_EA)/cumsum(.99^t),
                                               RegType_RoW_EA_hist = cumsum(.99^t * RegType_RoW_EA)/cumsum(.99^t),
                                               Politytype_Anocracy_hist = cumsum(.99^t * Politytype_Anocracy)/cumsum(.99^t),
                                               status_fh_PF_hist = cumsum(.99^t * status_fh_PF)/cumsum(.99^t),
                                               AnckarRegtype_MP_Autocracy_hist = cumsum(.99^t * AnckarRegtype_MP_Autocracy)/cumsum(.99^t),
                                               HTW_RegType_MP_Autocracy_hist = cumsum(.99^t * HTW_RegType_MP_Autocracy)/cumsum(.99^t),
                                               RegType_miller_EA_hist = cumsum(.99^t * RegType_miller_EA)/cumsum(.99^t),
                                               RegType_magaloni_EA_hist = cumsum(.99^t * RegType_magaloni_EA)/cumsum(.99^t),
                                               RegType_lied_DEM_hist = cumsum(.99^t * RegType_lied_DEM)/cumsum(.99^t),
                                               RegType_RoW_DEM_hist = cumsum(.99^t * RegType_RoW_DEM)/cumsum(.99^t),
                                               Politytype_Democracy_hist = cumsum(.99^t * Politytype_Democracy)/cumsum(.99^t),
                                               status_fh_F_hist = cumsum(.99^t * status_fh_F)/cumsum(.99^t),
                                               AnckarRegtype_Democracy_hist = cumsum(.99^t * AnckarRegtype_Democracy)/cumsum(.99^t),
                                               HTW_RegType_Democracy_hist = cumsum(.99^t * HTW_RegType_Democracy)/cumsum(.99^t),
                                               RegType_miller_DEM_hist = cumsum(.99^t * RegType_miller_DEM)/cumsum(.99^t),
                                               RegType_magaloni_DEM_hist = cumsum(.99^t * RegType_magaloni_DEM)/cumsum(.99^t))





save(df, file = "Data/Replication/Miller/Miller_Replication_Ready.rda")


